/**
 * ManageAccount.java 2008-05-14
 * 
 * Linker is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

package linker.account;

import java.awt.Image;
import java.awt.Toolkit;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Manage local accounts information Save to or load from local drivers Get
 * local path of Linker and resources.
 * 
 * @version 2008-05-19
 * @author Jianfeng tujf.cn@gmail.com
 * @author AwakenRz awakenrz@gmail.com
 * 
 */

public class ManageAccount extends Object {

	/**
	 * The path of "resources\".
	 */
	private static String resourcesPath;

	/**
	 * The path of "resources\config\".
	 */
	private static String userinfoFile;

	/**
	 * The logo.
	 */
	private static Image logo;

	/**
	 * The user accounts of Linker.
	 */
	private static Properties accounts = new Properties();

	/**
	 * The blank default constructor.
	 */
	public ManageAccount() {
	}

	/**
	 * getAccounts() to get Accounts information.
	 * 
	 * @return the accounts information
	 */
	public static Properties getAccounts() {
		getPath();

		try {
			userinfoFile = getConfigPath() + "user.info";
			File info = new File(userinfoFile);
			if (!info.exists()) {
				info.createNewFile();
			}

			accounts.load(new FileInputStream(userinfoFile));
		} catch (Exception e) {
			e.printStackTrace();
		}

		return accounts;
	}

	/**
	 * Add the new account information.
	 * 
	 * @param username
	 *            Username to be saved.
	 * @param password
	 *            Password to be saved.
	 * @param host
	 *            Server host to be saved.
	 * @param port
	 *            Server port to be saved.
	 * @param domain
	 *            Server domain to be saved.
	 */
	public static void addAccount(final String username, final String password,
			final String host, final String port, final String domain) {

		if (port != null) {
			accounts.put(username + ".port", port);
		}
		if (host != null) {
			accounts.put(username + ".host", host);
		}
		if (domain != null) {
			accounts.put(username + ".domain", domain);
		}

		accounts.put(username, password);

		try {
			accounts.store(new FileOutputStream(userinfoFile), "UserInfo");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Get the SourcePath.
	 * 
	 * @return sourcePath
	 */
	public static String getPath() {
		if (resourcesPath != null) {
			return resourcesPath;
		}

		resourcesPath = "resources" + File.separator;

		// Judge whether it is UNIX like system. and get the absolute path to
		// avoid run program
		if (File.separator.endsWith("/")) {
			String temp = ManageAccount.class.getResource("").toString();
			if (temp.indexOf("Linker.jar") >= 0) {
				temp = temp.substring(temp.indexOf("/"), temp
						.indexOf("Linker.jar"));
				resourcesPath = temp + resourcesPath;
			}
		}

		System.out.println(resourcesPath);

		return resourcesPath;
	}

	/**
	 * Get image path.
	 * 
	 * @return The path of "resources\images\"
	 */
	public static String getImagePath() {
		return getPath() + "images" + File.separator;
	}

	/**
	 * Get config path.
	 * 
	 * @return The path of "resources\config\"
	 */
	public static String getConfigPath() {
		return getPath() + "config" + File.separator;
	}

	/**
	 * Get plugins path.
	 * 
	 * @return The full path of "resources\plugins".
	 */
	public static String getPluginPath() {
		return getPath() + "plugins" + File.separator;
	}

	/**
	 * Get Linker logo.
	 * 
	 * @return Linker logo.
	 */
	public static Image getLogo() {
		if (logo == null) {
			logo = Toolkit.getDefaultToolkit().createImage(
					getImagePath() + "logo.png");
		}

		return logo;
	}

}
